//
//  CourseClass.m
//  Courses
//
//  Created by xxx on 15/1/9.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "CourseClass.h"
#import "Request.h"

@implementation CourseClass

- (id)initWithJsonDict:(NSDictionary *)jsonDict
{
    if (self = [super init])
    {
        self.classId = jsonDict[@"classId"];
        self.classPrice = jsonDict[@"classPrice"];
        self.classOpenDate = jsonDict[@"classOpenDate"];
        self.lessonBeginDate = jsonDict[@"lessonBeginDate"];
        id jsonArray = jsonDict[@"lessons"];
        if ([jsonArray isKindOfClass:[NSArray class]])
        {
            NSMutableArray *lessonArray = [[NSMutableArray alloc] init];
            for (id dict in jsonArray)
            {
                if ([dict isKindOfClass:[NSDictionary class]])
                {
                    Lesson *lesson = [[Lesson alloc] initWithJsonDict:dict];
                    [lessonArray addObject:lesson];
                }
            }
            self.lessons = [lessonArray copy];
        }
    }
    return self;
}

+ (NSArray *)courseArrangementByCourseId:(NSString *)courseId
{
    if (!courseId.length) return nil;
    
    Request *request = [Request requestWithName:@"get_lessons"];
    request.body[@"courseId"] = courseId;
    
    Response *response = [request response];
    
    NSMutableArray *classes = [[NSMutableArray alloc] init];
    if (response.result)
    {
        for (id jsonDict in response.content[@"classes"])
        {
            if ([jsonDict isKindOfClass:[NSDictionary class]])
            {
                CourseClass *cls = [[CourseClass alloc] initWithJsonDict:jsonDict];
                [classes addObject:cls];
            }
        }
    }
    
    return [classes copy];
}

@end
